package cn.dada.dadarecipes.mall.cart.controller;

import cn.dada.dadarecipes.base.response.JsonResult;
import cn.dada.dadarecipes.mall.cart.pojo.dto.CartAddParam;
import cn.dada.dadarecipes.mall.cart.pojo.dto.CartDeleteParam;
import cn.dada.dadarecipes.mall.cart.pojo.dto.CartUpdateParam;
import cn.dada.dadarecipes.mall.cart.pojo.dto.OrderAddDTO;
import cn.dada.dadarecipes.mall.cart.pojo.vo.CartProductVO;
import cn.dada.dadarecipes.mall.cart.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags ="03.03购物车模块")
@Slf4j
@RestController
@RequestMapping(value = "/v1/cart/")
public class CartController {
    @Autowired
    CartService cartService;

    @ApiOperation(value = "获取购物车列表")
    @GetMapping("select/{userId}")
    public JsonResult selectCart(@PathVariable Long userId) {
        log.debug("获取购物车列表,userId={}",userId);
        List<CartProductVO> list = cartService.selectCart(userId);
        return JsonResult.ok(list);
    }

    @ApiOperation("添加商品到购物车")
    @PostMapping("addProducts")
    public JsonResult addProducts(CartAddParam cartAddParam){
        log.debug("添加商品到购物车,cartAddParam={}",cartAddParam);
        cartService.addProducts(cartAddParam);
        return JsonResult.ok();
    }

    @ApiOperation(value ="更新购物车商品信息")
    @PostMapping("updateProducts")
    public JsonResult updateProducts(CartUpdateParam cartUpdateParam){
        log.debug("更新购物车商品数量,cartUpdateParam={}",cartUpdateParam);
        cartService.updateCartProducts(cartUpdateParam);
        return JsonResult.ok();
    }

    @ApiOperation(value ="删除购物车商品")
    @PostMapping("deleteProducts")
    public JsonResult deleteProducts(CartDeleteParam cartDeleteParam){
        log.debug("删除购物车商品,cartDeleteParam={}",cartDeleteParam);
        cartService.deleteProducts(cartDeleteParam);
        return JsonResult.ok();
    }

    @ApiOperation(value ="新增订单")
    @PostMapping("addOrder")
    public JsonResult addOrder(OrderAddDTO orderAddDTO){
        log.debug("新增订单,userId={}",orderAddDTO);
        cartService.addOrder(orderAddDTO);
        return JsonResult.ok();
    }


}
